package com.aube.plugin.detail;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.plugin.figure.CharacterActivity;
import com.aube.plugin.figureex.roundedimageview.RoundedImageView;
import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Category;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.utils.Utils;
import com.ryg.dynamicload.internal.DLIntent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by hill on 17/1/4.
 */

public class WidgetTalent extends DLLayout {
    private final String TAG = "Aube/FigureExGrid";

    private Context mContext;

    private IPositionManager mManager;
    private AtomicBoolean mAnimated = new AtomicBoolean(false);

    private View mFlag;
    private TextView mTitle;
    private RoundedImageView mTalentImg;

    private LinearLayout mOveralLy, mHeadLy;
    private LinearLayout mTalentsLy;
    private HorizontalScrollView mScrollView;

    private int mTalentIndex = 0;

    private static final int ANIM_DURATION = 300;
    private static final int ANIM_DELAY = 200;

    public WidgetTalent(Context context) {
        this(context, null);
    }
    public WidgetTalent(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public WidgetTalent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    @Override
    public void initViews() {
        mOveralLy = new LinearLayout(mContext);
        mOveralLy.setOrientation(LinearLayout.VERTICAL);

        mHeadLy = new LinearLayout(mContext);
        mHeadLy.setOrientation(LinearLayout.HORIZONTAL);
        mHeadLy.setPadding(Utils.dip2px(mContext, 15), Utils.dip2px(mContext, 30), Utils.dip2px(mContext, 15), Utils.dip2px(mContext, 20));

        mFlag = new View(mContext);
        mFlag.setAlpha(0);
        mFlag.setBackgroundColor(mContext.getResources().getColor(R.color.theme));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 2), Utils.sp2px(mContext, 14));
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        mHeadLy.addView(mFlag, layoutParams);

        mTitle = new TextView(mContext);
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTextSize(14);
        mTitle.setAlpha(0);
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = Utils.dip2px(mContext, 10);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        mHeadLy.addView(mTitle, layoutParams);

        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        mOveralLy.addView(mHeadLy, layoutParams);

        mTalentsLy = new LinearLayout(mContext);
        mTalentsLy.setOrientation(LinearLayout.HORIZONTAL);
        mTalentsLy.setPadding(Utils.dip2px(mContext, 15), 0, Utils.dip2px(mContext, 15), Utils.dip2px(mContext, 30));

        mScrollView = new HorizontalScrollView(mContext);
        mScrollView.setHorizontalScrollBarEnabled(false);
        HorizontalScrollView.LayoutParams layoutParams1 = new HorizontalScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mScrollView.addView(mTalentsLy, layoutParams1);

        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mOveralLy.addView(mScrollView, layoutParams);

        addView(mOveralLy, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    @Override
    public void fillData(Object object) {
        if(object == null || !(object instanceof Category)) {
            Utils.Log(TAG, "[fillData] object is null or not instance of Category");
            return;
        }

        Category category = (Category) object;
        mTitle.setText(category.modelName);

        List list = (List) category.dataDetail;
        if(list == null)
            return;

        List<Character> characters = new ArrayList<>();
        Character cItem;
        for(Object obj : list) {
            cItem = new Character();
            LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
            Iterator<String> keyIterator = lhm.keySet().iterator();
            while (keyIterator.hasNext()) {
                String key = keyIterator.next();
                Object value = lhm.get(key);

                if(Character.ACTOR_NAME.equalsIgnoreCase(key)) {
                    cItem.actorName = (String) value;
                } else if(Character.ACTOR_PIC.equalsIgnoreCase(key)) {
                    cItem.actorPic = (String) value;
                } else if("actorDesc".equalsIgnoreCase(key)) {
                    cItem.actorDesc = (String) value;
                } else if("guestName".equalsIgnoreCase(key)) {
                    cItem.actorName = (String) value;
                } else if("guestPic".equalsIgnoreCase(key)) {
                    cItem.actorPic = (String) value;
                } else if(Character.PRAISE_COUNT.equalsIgnoreCase(key)) {
                    cItem.praiseCount = (int) ((double) value);
                } else if(Character.RELATED_ID.equalsIgnoreCase(key)) {
                    cItem.relatedId = (String) value;
                } else if("guestDesc".equalsIgnoreCase(key)) {
                    cItem.actorDesc = (String) value;
                } else if("praised".equalsIgnoreCase(key)) {
                    cItem.praised = (String) value;
                } else if("period".equalsIgnoreCase(key)) {
                    cItem.period = (String) value;
                } else if("isNotClick".equalsIgnoreCase(key)) {
                    cItem.isNotClick = (String) value;
                }
            }
            characters.add(cItem);
        }

        for(final Character character : characters) {
            LinearLayout talentLy = new LinearLayout(mContext);
            talentLy.setOrientation(LinearLayout.VERTICAL);
            talentLy.setAlpha(0);

            RoundedImageView talentImg = new RoundedImageView(mContext);
            talentImg.setOval(true);
            talentImg.setCornerRadius(Utils.dip2px(mContext, 60));
            talentImg.setBackground(mContext.getResources().getDrawable(R.drawable.bg_roundimg));
            talentImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
            if(character.actorPic != null)
                ImageLoader.getInstance(mContext).fillImage(talentImg, character.actorPic, Utils.dip2px(mContext, 60), Utils.dip2px(mContext, 60));
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(mContext, 60), Utils.dip2px(mContext, 60));
            talentLy.addView(talentImg, layoutParams);

            TextView talentName = new TextView(mContext);
            talentName.setTextColor(Color.WHITE);
            talentName.setTextSize(12);
            talentName.setText(character.actorName);
            talentName.setGravity(Gravity.CENTER);
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = Utils.dip2px(mContext, 10);
            layoutParams.gravity = Gravity.CENTER;
            talentLy.addView(talentName, layoutParams);

            TextView talentDesc = new TextView(mContext);
            talentDesc.setTextColor(mContext.getResources().getColor(R.color.white_30));
            talentDesc.setTextSize(10);
            talentDesc.setText(character.actorDesc);
            talentDesc.setGravity(Gravity.CENTER);
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = Utils.dip2px(mContext, 5);
            layoutParams.gravity = Gravity.CENTER;
            talentLy.addView(talentDesc, layoutParams);

            talentLy.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.figure.CharacterActivity");
                    intent.putExtra(CharacterActivity.ACTOR, character);
                    mBridge.openPluginActivity(intent);
                }
            });

            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.rightMargin = Utils.dip2px(mContext, 10);
            mTalentsLy.addView(talentLy, layoutParams);

            mTalentIndex++;
        }
    }

    public void setTheme(int color) {
        mFlag.setBackgroundColor(color);
    }

    @Override
    public void notifyAnim(int animDelay) {
        if(mManager == null)
            return;
        if(mAnimated.get())
            return;
        mAnimated.set(true);
        //mLayoutGroup.setVisibility(View.VISIBLE);
        if(animDelay > 0)
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    startSlideInAnimComb();
                }
            }, animDelay);
        else
            startSlideInAnimComb();
    }

    private void startSlideInAnimComb() {
        ObjectAnimator alphaTitle = ObjectAnimator.ofFloat(mTitle, "alpha", 0, 1);
        alphaTitle.setDuration(ANIM_DURATION);

        ObjectAnimator translate = ObjectAnimator.ofFloat(mFlag, "translationY", -mFlag.getHeight(), 0);
        translate.setDuration(ANIM_DURATION);
        translate.setStartDelay(ANIM_DELAY);
        translate.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mFlag.setAlpha(1f);
            }
        });

        AnimatorSet mSet = new AnimatorSet();
        mSet.playTogether(alphaTitle, translate);
        mSet.start();

        for(int i = 0; i < mTalentsLy.getChildCount(); i++) {
            LinearLayout layout = (LinearLayout) mTalentsLy.getChildAt(i);

            ObjectAnimator imgAlpha = ObjectAnimator.ofFloat(layout, "alpha", 0, 1f);
            imgAlpha.setDuration(300);
            imgAlpha.setStartDelay(ANIM_DURATION + i * ANIM_DELAY);

            ObjectAnimator imgTranslate = ObjectAnimator.ofFloat(layout, "translationX", layout.getMeasuredWidth() / 2, 0);
            imgTranslate.setDuration(300);
            imgAlpha.setStartDelay(ANIM_DURATION + i * ANIM_DELAY);

            mSet = new AnimatorSet();
            mSet.playTogether(imgAlpha, imgTranslate);
            mSet.start();
        }
    }

    @Override
    public void registToManager(IPositionManager manager) {
        if(mManager != null || manager == null)
            return;
        manager.add(this);
        this.mManager = manager;

        //mLayoutGroup.setVisibility(View.INVISIBLE);
    }

    @Override
    public void unreistFromManager(IPositionManager manager) {
        if(mManager == null || manager == null)
            return;
        manager.remove(this);
        this.mManager = null;
    }

    @Override
    public int[] getLocations() {
        int[] locs = new int[2];
        getLocationOnScreen(locs);
        return locs;
    }
}
